Write a JavaScript program to getthe length of a JavaScript object.
let student = {
name : "John Doe",
class : "JavaScript",
rollno : 12 }
let student = {
name: "John Doe",
class: "JavaScript",
rollno: 12
};
const myFunc1 = obj => Object.keys(obj).length;
document.getElementById("ex1").innerText = myFunc1(student);
Write a JavaScript function to get all the values of an object's values
and return a single String of values as an example.
let bio = {name : "Dcoders", course: "JavaScript"}
//Output : "Dcoders JavaScript"
let bio = { name: "Dcoders", course: "JavaScript" };
const myFunc2 = obj => Object.values(obj).join(" ");
document.getElementById("ex2").innerText = myFunc2(bio);
Create a function to where I can pass Object and Property. Return
Boolean value if property exists in the given object.
let checkKey( {name:"abc"}, "name") // Output : true
let checkKey( {age:10, color:"red"}, "course"); // Output : false
const myFunc3 = (obj, prop) => prop in obj;
document.getElementById("ex3").innerText = myFunc3({name:"abc"},"name");
Change the value of given object.Concat "Yes" to the value and return new value of an object.
let changeVaIue = {name: "dcoders"} // Output : "yes dcoders"
const myFunc4 = obj => "Yes " + Object.values(obj) ;
document.getElementById("ex4").innerText = myFunc4({name:"dcoders"});
Using Destructuring method, return a value of name key from object.
let getName = { age:20, course:"JavaScript", name:"John Doe"};
Output : John Doe
let getName = { age: 20, course: "JavaScript", name: "John Doe" };
const myFunc5 = obj => {
const { name } = obj;
return name;
}
document.getElementById("ex5").innerText = myFunc5(getName);
Remove the "color" property from an object and return a string with
remaining key value pairs of an object.
let removeKey({name:"John Doe",age:20,color:"pink"});
Output :- name John doe age 20
let removeKey = { name: "John Doe", age: 20, color: "pink" };
const myFunc6 = obj => {
delete obj.color;
return Object.entries(obj).flat(Infinity).join(" ")
}
document.getElementById("ex6").innerText = myFunc6(removeKey);
Create a functions which return value of city from nested object.
let person = {
name: "John Doe",
address: {
city: "Springfield",
}
}
let person = {
name: "John Doe",
address: {
city: "Springfield"
}
}
const myFunc7 = obj => obj.address.city;
document.getElementById("ex7").innerText = myFunc7(person);
Create a functions to return an Object from given nested
Array.
let GetObj([['age',20],['color','red']]);
{age:20,color:"red"};
const myFunc8 = arr => Object.fromEntries(arr);
document.getElementById("ex8").innerText = JSON.stringify(myFunc8([['age', 20], ['color', 'red']]));
Create a function to takes an Object and return true if value of any
key is of Number Datatype.
let checkNumVaIue ({age: 20}); Output: true
let checkNumVaIue ({age: "20"}); Output: false
const myFunc9 = obj =>
Object.values(obj).some((ele => typeof ele == 'number'))
document.getElementById('ex9').innerText = myFunc9({ age: "90", age2: 2 })
Create a function which takes an Array of an Objects and return a
String with all the properties of each object.
getStr({name:'John, age:20}, {type:'vehicle', model: 'car'});
Output : "name age type model"
const myFunc9 = obj =>
Object.values(obj).some((ele => typeof ele == 'number'))
document.getElementById('ex9').innerText = myFunc9({ age: "90", age2: 2 })
//exercise 10
let tempVar = [{name:'John', age:20}, {type:'vehicle', model: 'car'},{year:2019,BuiltYear:1991}];
let newStr = [];
const myFunc10 = arr => {
arr.forEach(e =>{
newStr.push(Object.keys(e).flat(Infinity).join(" "))
} );
return newStr.join(" ");
}
document.getElementById('ex10').innerText = myFunc10(tempVar);